package com.example.lxx.stickyexpandview;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.lxx.stickyexpandview.model.EsalerAllCustomerModel;
import com.example.lxx.stickyexpandview.model.EsalerPermissionCustomersModel;
import com.example.lxx.stickyexpandview.model.EsalerPermissionTittleModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxx on 2017/10/30 0030.
 */

public class CustomersAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    public static final int TYPE_LEVEL_2 = 2;

    private List mTList = new ArrayList();
    private ImageView rlFirstArrow;


    public CustomersAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.first_item_view);
        addItemType(TYPE_LEVEL_1, R.layout.group_item_view);
        addItemType(TYPE_LEVEL_2, R.layout.child_item_view);
        mTList = data;
    }

    @Override
    protected void convert(final com.chad.library.adapter.base.BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                 final EsalerAllCustomerModel model0 =(EsalerAllCustomerModel) item;
                helper.setText(R.id.tv_first_tittle_name,model0.getName());

                rlFirstArrow = helper.getView(R.id.iv_first_arrow);

                rlFirstArrow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickFirstExpandListener != null){
                            onClickFirstExpandListener.expandFirst(helper.getLayoutPosition(),(ImageView)(helper.getView(R.id.iv_first_arrow)),model0);
                        }
                    }
                });
                if (model0.isExpanded()){
                    helper.setImageResource(R.id.iv_first_arrow,R.mipmap.esaler_up_arrow);
                }else {
                    helper.setImageResource(R.id.iv_first_arrow,R.mipmap.esaler_down_arrow);
                }
                break;

            case TYPE_LEVEL_1:
                 final EsalerPermissionTittleModel model1 = (EsalerPermissionTittleModel) item;

                helper.setImageResource(R.id.iv_second_check,model1.getIsSelected() ? R.mipmap.esaler_icon_checked:R.mipmap.esaler_icon_unchecked);
                helper.setText(R.id.tv_second_tittle_name,model1.getGroup_name());

                helper.getView(R.id.iv_second_arrow).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickSecondExpandListener != null){
                            onClickSecondExpandListener.expandSecond(helper.getLayoutPosition(),(ImageView) (helper.getView(R.id.iv_second_check)),
                                    (ImageView)(helper.getView(R.id.iv_second_arrow)),model1);
                        }
                    }
                });

                helper.getView(R.id.iv_second_check).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onGroupCheckedClickListener != null){
                            onGroupCheckedClickListener.groupCheckedClickListener(helper.getLayoutPosition(),model1,model1.getIsSelected());
                        }
                    }
                });

                if (model1.isExpanded()){
                    helper.setImageResource(R.id.iv_second_arrow,R.mipmap.esaler_up_arrow);
                }else {
                    helper.setImageResource(R.id.iv_second_arrow,R.mipmap.esaler_down_arrow);
                }
                break;

            case TYPE_LEVEL_2:
                  final EsalerPermissionCustomersModel model2 = (EsalerPermissionCustomersModel) item;

                helper.setImageResource(R.id.iv_child_check,model2.getIsSelected() ? R.mipmap.esaler_icon_checked:R.mipmap.esaler_icon_unchecked);
                helper.setImageResource(R.id.iv_head,"0".equals(model2.getHead_portrait())? R.mipmap.esaler_picture_man : R.mipmap.esaler_picture_woman);

                helper.setText(R.id.tv_content,model2.getName()+"("+model2.getMobile()+")");
                helper.setText(R.id.tv_address,model2.getProvince()+model2.getCity());
                helper.setText(R.id.tv_lack,model2.getIs_notfull_name());

                helper.getView(R.id.rl_checked).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onChildItemClickListener != null){
                            onChildItemClickListener.childItemClick(helper.getLayoutPosition(),model2,model2.getIsSelected());
                        }
                    }
                });
                break;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    OnGroupCheckedClickListener onGroupCheckedClickListener;

    OnChildItemClickListener onChildItemClickListener;

    OnClickFirstExpandListener onClickFirstExpandListener;

    OnClickSecondExpandListener onClickSecondExpandListener;

    public interface OnClickSecondExpandListener{
        void expandSecond(int position, ImageView ivCheck, ImageView ivArrow, EsalerPermissionTittleModel model);
    }
    public interface OnGroupCheckedClickListener{
        void groupCheckedClickListener(int position, EsalerPermissionTittleModel model, boolean isCheck);
    }

    public interface OnChildItemClickListener{
        void childItemClick(int position, EsalerPermissionCustomersModel model, boolean isCheck);
    }

    public interface OnClickFirstExpandListener{
        void expandFirst(int position, ImageView imageView, EsalerAllCustomerModel model);
    }

    public void setOnClickFirstExpandListener(OnClickFirstExpandListener onClickFirstExpandListener) {
        this.onClickFirstExpandListener = onClickFirstExpandListener;
    }

    public void setOnClickSecondExpandListener(OnClickSecondExpandListener onClickSecondExpandListener) {
        this.onClickSecondExpandListener = onClickSecondExpandListener;
    }

    public void setOnChildItemClickListener(OnChildItemClickListener onChildItemClickListener) {
        this.onChildItemClickListener = onChildItemClickListener;
    }

    public void setOnGroupCheckedClickListener(OnGroupCheckedClickListener onGroupCheckedClickListener) {
        this.onGroupCheckedClickListener = onGroupCheckedClickListener;
    }
}
